package com.century.sjt.adapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.century.sjt.R;
import com.century.sjt.entity.BankCard;
import com.century.sjt.util.Constant;

import java.util.List;

/**
 * Created by 炳基 on 2016/7/26.
 */
public class ChooseBackCardAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater;
    // 映射数据
    private List<BankCard> mDataList;
    private Context context;
    private String recommendName;
    private String isShowCreditCard;//是否显示信用卡
    private Handler mHandler;


    public ChooseBackCardAdapter(Context context, List<BankCard> list, Handler mHandler, String isShowCreditCard) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        mDataList = list;
        this.mHandler = mHandler;
        this.isShowCreditCard = isShowCreditCard;
    }

    // 获取数据量
    @Override
    public int getCount() {
        return mDataList.size();
    }

    // 获取对应ID项对应的Item
    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    // 获取对应项ID
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();

            //由于我们只需要将XML转化为View，并不涉及到具体的布局，所以第二个参数通常设置为null
            convertView = mLayoutInflater.inflate(R.layout.sjt_chosepay_item, null);
            holder.tvBankName = (TextView) convertView.findViewById(R.id.tv_bankname);
            holder.tvBankCardNo = (TextView) convertView.findViewById(R.id.tv_bankcardno);
            holder.tvBankImage = (ImageView) convertView.findViewById(R.id.a_yh_image);
            holder.CheckLayout = (LinearLayout) convertView.findViewById(R.id.ly_chackbank);
//            holder.cbCheck = (RadioButton) convertView.findViewById(R.id.cb_check);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

//        holder.cbCheck.setChecked(mDataList.get(position).getCheck());
//        holder.cbCheck.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                for (int i = 0; i < mDataList.size(); i++) {
//                    mDataList.get(i).setCheck(false);
//                }
//                mDataList.get(position).setCheck(true);
//                Message msg = new Message();
//                msg.what = 1;
//                mHandler.sendMessage(msg);
//            }
//        });


        holder.tvBankName.setText(mDataList.get(position).getCardName());
        String cardType=mDataList.get(position).getCardType();
        String cardNo=mDataList.get(position).getBankCardNo();
//        Log.e("cardNo",cardNo);
        if(cardNo.indexOf(".")!=-1){
            cardNo="￥"+cardNo;
        }else{
//            cardNo = cardNo.substring(cardNo.length()-4);
//            holder.tvBankCardNo.setVisibility(View.GONE);
            cardNo = "";
        }
//        cardNo="1234567890";

        holder.tvBankCardNo.setText(cardNo);

//        默认图标
        holder.tvBankImage.setImageResource(R.mipmap.yl);

        String[] bankCodes = context.getResources().getStringArray(R.array.bankcode);
        TypedArray ar = context.getResources().obtainTypedArray(R.array.bankicon);
        for(int i = 0;i<bankCodes.length;i++){
//            Log.e("bankCodes",""+(bankCodes[i].toString()==mDataList.get(position).getBankCode().toString()));
            if(bankCodes[i].toString().equals(mDataList.get(position).getBankCode().toString())){
                holder.tvBankImage.setImageResource(ar.getResourceId(i, 0));
//                Log.e("ar",""+ar.getResourceId(i, 0));
                break;
            }
        }

        if(isShowCreditCard.equals("0")) {//是否显示信用卡
            if (cardType.equals(Constant.CREDIT_CARD)) {
//            holder.CheckLayout.setEnabled(false);
                holder.CheckLayout.setBackgroundColor(Color.rgb(220, 220, 220));
            } else {
                holder.CheckLayout.setBackgroundColor(Color.rgb(255, 255, 255));
            }
        }else {
            holder.CheckLayout.setBackgroundColor(Color.rgb(255, 255, 255));
        }

        return convertView;
    }
    // ViewHolder用于缓存控件
    class ViewHolder {
        public TextView tvBankName;
        public TextView tvBankCardNo;
        public ImageView tvBankImage;
        public RadioButton cbCheck;
        public LinearLayout CheckLayout;

    }
}
